# -*- coding: utf-8 -*-
# @Author : zhaochen
# @Date   : 2019/2/14
# @File   : R13_IC_UpaidLoanCurrOvduAmtCheck.py
# @E-mail : zhaochen@bbdservice.com

'''eof
name:申请人未结清贷款当前逾期校验
code:R13_IC_UpaidLoanCurrOvduAmtCheck
tableName:
columnName:
groups:个人征信校验
dependencies:IC_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import re
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(table_Name):
    try:
        data = IC_DSJ['data'].get(table_Name)
        if data in null_type_list:
            return None
        else:
            # 存在1条为空的数据[{}]
            if isinstance(data, list) and len(data) == 1 and data[0]['reportNo'] in null_type_list:
                return [{}]
            # 存在空list []
            elif isinstance(data, list) and len(data) == 0:
                return None
            else:
                return data
    except:
        return None

def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

def R13_IC_UpaidLoanCurrOvduAmtCheck():
    Factor = 0
    flag = []
    loanInfo002 = getTable('ICRLoanInfo002*Loan')
    if loanInfo002 is None:
        return u'缺失值'
    elif loanInfo002 == [{}]:
        return 0
    else:
        for i in loanInfo002:
            try:
                if i['state'] in [u'正常', u'逾期', u'呆账'] and isfloat(i['currOverdueAmount']) \
                        and float(i['currOverdueAmount']) > 0:
                    Factor += 1
            except:
                flag.append(None)
                continue
        if len(flag) == len(loanInfo002):
            return u'缺失值'
        else:
            return Factor

result = R13_IC_UpaidLoanCurrOvduAmtCheck()

